package com.gexin.common.utils;

import java.util.ArrayList;
import java.util.List;

public class ListPage {


    /**
     * 根据总条数获取总页数
     * @param pageSize
     * @param totalSize
     * @return
     */
    public static int getTotalPage(int pageSize,int totalSize){
        int totalPage = 0;
        if(totalSize % pageSize == 0){
            totalPage = totalSize / pageSize;
        }else{
            totalPage = totalSize / pageSize+1;
        }
        return totalPage;
    }

    /**
     * 循环截取某页列表进行分页
     *
     * @param list    分页数据
     * @param pageSize    页面大小
     * @param pageNum 当前页面
     */
    public static List listToPage(int pageNum, int pageSize, List list){
        int limit = (pageNum-1)*pageSize;
        int size = list.size();
        int totalPage = getTotalPage(pageSize,size);
        if(pageNum > totalPage){
            System.out.println("页数超出了");
            throw new RuntimeException("页数超出了");
        }
        List<Integer> subList = null;
        if(pageNum == totalPage){
            System.out.println("最后一页");
            subList = list.subList(limit, size);
        }else{
            int end= limit + pageSize;
            System.out.println("截取的最后的下标 = " + end);
            subList = list.subList(limit, end);
        }
//        subList.removeIf(
//                e->e.getClass().getName().equals("java.util.ArrayList")
//        );

        return subList;
    }
 }
